package com.probiz.estore.content.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.common.model.content.ContentRelatedType;
import com.probiz.estore.common.model.sales.RecommendedType;
import com.probiz.estore.content.service.AutoEvalRelatedContentManager;
import com.probiz.estore.content.service.ContentRelatedTypeManager;
import com.probiz.estore.content.service.EvalRelatedContentManager;
import com.probiz.estore.content.service.RelatedContentManager;

public class EvalRelatedContentManagerImpl implements EvalRelatedContentManager{

	private ContentRelatedTypeManager contentRelatedTypeManager;
	private RelatedContentManager relatedContentManager;
	private AutoEvalRelatedContentManager autoEvalRelatedContentManager;
	public void setContentRelatedTypeManager(
			ContentRelatedTypeManager contentRelatedTypeManager) {
		this.contentRelatedTypeManager = contentRelatedTypeManager;
	}
	

	public void setRelatedContentManager(RelatedContentManager relatedContentManager) {
		this.relatedContentManager = relatedContentManager;
	}
	

	public void setAutoEvalRelatedContentManager(
			AutoEvalRelatedContentManager autoEvalRelatedContentManager) {
		this.autoEvalRelatedContentManager = autoEvalRelatedContentManager;
	}


	public List<Content> getContentsByRecommendedTypeNameBySourceId(
			String relatedTypeName, int sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		
		ContentRelatedType contentRelatedType = contentRelatedTypeManager.getContentRelatedTypeByName(relatedTypeName);
		if (null == contentRelatedType)
			return new ArrayList<Content>();
		if (Constants.FLAG_TRUE.equals(contentRelatedType.getStatus())) {
			// 获得手动评估的文章
			List<Content> manulEvalContentsList = relatedContentManager.getContentsByRelatedTypeNameBySourceId(relatedTypeName, sourceKind, sourceId, firstResult, maxResults);
			if (maxResults == manulEvalContentsList.size()) {
				// 数量已经满足
				return manulEvalContentsList;
			} else {
				// 数量还不够
				if (RecommendedType.AUTOEVAL_YES.equals(contentRelatedType.getAutoEval())) {
					// 如果开启自动评估功能
					List<Content> autoEvalContentsList = autoEvalRelatedContentManager.getContentsByRuleCodeBySourceId(contentRelatedType.getRuleCode(), sourceId, firstResult, maxResults);
					return mergeList(manulEvalContentsList,
							autoEvalContentsList, maxResults);
				} else {
					return manulEvalContentsList;
				}
			}
		} else {
			return new ArrayList<Content>();
		}
	}

	public List<Content> getContentsByRecommendedTypeNameBySourceIdList(
			String relatedTypeName, int sourceKind, List<Integer> sourceIds,
			int firstResult, int maxResults) {
		
		return new ArrayList<Content>();
	}
	
	/**
	 * 将输入参数的两个list合并成一个大小为maxResult的新list
	 * 
	 * @param manulEvalContentsList
	 * @param autoEvalContentsList
	 * @param maxResults
	 * @return List<Content>
	 */
	private List<Content> mergeList(List<Content> manulEvalContentsList,
			List<Content> autoEvalContentsList, int maxResults) {
		List<Content> list = new ArrayList<Content>();
		list.addAll(manulEvalContentsList);
		for (int i = 0; i < autoEvalContentsList.size(); i++) {
			Content c = (Content) autoEvalContentsList.get(i);
			boolean add = true;
			for (int j = 0; j < list.size(); j++) {
				Content rc = (Content) list.get(j);
				if (c.getContentId().equals(rc.getContentId())) {
					add = false;
					break;
				}
			}
			if (add) {
				list.add(c);
				if (list.size() == maxResults) {
					break;
				}
			}
		}
		return list;
	}
	
}
